namespace Plugins.Engine
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.CodeAnalysis;
    using Plugins.Core.Interfaces;
    using Plugins.Interfaces;

    [Serializable]
    public class PluginServiceProvider<T> : IPluginServiceProvider<T> where T : class, IPluginServiceProvider<T>
    {
        private readonly Guid _id;// = typeof(T).GUID;//Guid.NewGuid();

        public Guid Id { get { return _id; } }

        private static IPluginService<T> _service = new PluginService<T>();

        public PluginServiceProvider(Guid value)
        {
            _id = value;
        }

        public PluginServiceProvider()
        {
            _id = typeof(T).GUID;
        }

        public virtual void Initialize(IPluginServiceManager manager)
        {
            _service.Register(manager, this as T);
        }

        public virtual void Finalize(IPluginServiceManager manager)
        {
            _service.Unregister(manager, this as T);
        }

        public virtual void Register(IPluginServiceManager manager)
        {
            PluginServiceManager.RegisterService<T>(manager, _service);
        }

        public virtual void Unregister(IPluginServiceManager manager)
        {
            PluginServiceManager.UnregisterService<T>(manager, _service);
        }

        public IPluginService<T> PluginService
        {
            get
            {
                return _service;
            }
        }

        protected IPluginObjectCollection<T> GetObjectCollection(IPluginManager manager)
        {
            if (_service != null && manager != null)
            {
                return GetObjectCollection(manager.HostManager);
            }
            return null;
        }
        protected IEnumerable<T> GetServiceCollection(IPluginManager manager)
        {
            if (_service != null && manager != null)
            {
                return GetServiceCollection(manager.HostManager);
            }
            return null;
        }

        [SuppressMessage("Microsoft.Design", "CA1000")]
        public static IPluginObjectCollection<T> GetObjectCollection(IPluginHostManager manager)
        {
            if (manager != null)
            {
                return PluginObjectManager.GetObjectCollection<T>(manager);
            }
            return null;
        }

        [SuppressMessage("Microsoft.Design", "CA1000")]
        public static IEnumerable<T> GetServiceCollection(IPluginHostManager manager)
        {
            if (manager != null)
            {
                return PluginObjectManager.GetServiceCollection<T>(manager);
            }
            return null;
        }

        protected void ProcessObjects(Action<T> action, IPluginManager manager)
        {
            IPluginObjectCollection<T> collection = GetObjectCollection(manager);
            if (collection != null)
            {
                collection.ProcessObject(action);
            }
        }
    }
}

